<template>
  <!-- 添加部门对话框 -->
  <!-- title 对话框的标题 -->
  <!-- visible显示隐藏对话框 -->
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="dialogTitle"
    :visible="isShow"
    @close="handleCose"
    @open="GetSImpleUserList"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="handleSubmit">确定</el-button>
      <el-button size="small" @click="handleCose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetSImpleUserList, reqAddDepartment, reqAddDepartDetaiById, reqUpdateDepartment } from '@/api/department'
export default {

  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    nodedata: {
      type: Object,
      required: true
    },
    nodelist: {
      type: Array,
      required: true
    }
  },
  data() {
    // value 用户输入的内容
    const validateName = (rule, value, callback) => {
      // console.log(value, this.nodedata.id, this.nodelist)
      let res = []
      if (this.form.id) {
        if (value === this.nodedata.name) {
          // 编辑下 名称可以和原来相同
          callback()
          return
        }
        // 编辑 找的是兄弟部门 大家(pid相同)
        res = this.nodelist.filter(item => item.pid === this.nodedata.id)
      } else {
        // 根据父级部门找到子部门
        res = this.nodelist.filter(item => item.pid === this.nodedata.id)
      }
      // 有一个名字重复就会报错
      const reqeat = res.some(item => item.name === value)
      reqeat ? callback(new Error('同级部门已经出现该名字')) : callback()
    }
    const validateCode = (rule, value, callback) => {
      if (this.form.id && value === this.nodedata.code) {
        // 编辑且  输入内容与原来一致=>直接通过
        callback()
        return
      }
      const reqeat = this.nodelist.some(item => item.code === value)
      reqeat ? callback(new Error('同级部门该编码已重复')) : callback()
    }
    return {
      form: {
        // 为了发请求方便,字段名跟后端一致
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        // 必填/ 1-10
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门名称只能是1-10个字符', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码只能是1-10个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人必须选择', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍只能是1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      userList: []
      // dialogTitle: ''
    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑部门' : '添加子部门'
    }
  },

  created() {
    // this.GetSImpleUserList()
  },
  methods: {
    handleCose() {
    //  利用.sync 修饰符简化父传子的过程
    // 相当于父组件把数据的控制权交给了子组件
    // 子组件可以通过this.$emit('update:xxx', xxx)来修改数据
      // this.$emit('closeDialog', false)
      this.$emit('update:isShow', false)
      this.$refs.addForm.resetFields()
      // resetFields只能清空绑定表单之内的数据
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    // 获取部门负责人简单列表
    async GetSImpleUserList() {
      const { data } = await reqGetSImpleUserList()
      // console.log(data)
      this.userList = data
    },
    handleSubmit() {
      this.$refs.addForm.validate(async(flag) => {
        if (!flag) return
        if (this.form.id) {
          const res = await reqUpdateDepartment(this.form)
          console.log(res)
          this.$message.success('编码部门成功')
        } else {
          await reqAddDepartment({
            ...this.form,
            pid: this.nodedata.id
          })
          this.$message.success('添加子部门成功')
        }
        this.handleCose()
        this.$emit('refresh')
      })
    },
    async GetDepartDetaiById(id) {
      const { data } = await reqAddDepartDetaiById(id)
      console.log(data, '回显数据')
      // console.log(data)
      // 直接赋值即可回显,因为from已经双向绑定给了表单
      this.form = data
    }
  }
}
</script>

<style>

</style>
